|
|
|
|
|
|
|
Figure 4.5
You can use a text box to enter single lines of text or entire paragraphs. |
|
|
|
|
| |
|
|
|
|
When a text box is activated (known as receiving the focus) by the user tabbing to it or clicking in it, a common practice is for the contents to be selected (or highlighted). Although this feat cannot be accomplished automatically, it can be done pretty easily. Enter these lines of code in the text box's event procedure, replacing Text1 with the text box's actual name: |
|
|
|
|
|
|
|
|
Text1.SelLength = Len(Text1.Text) |
|
|
|
|
|
|
|
|
The SendKeys statement sends a string of characters to the active form at runtime just as if the users had typed them at the keyboard. In this case, you're acting as if the users had pressed the Home key and then a shifted End key (the plus sign before {End} represents Shift). This action causes the text to be highlighted, so the users can begin entering new text without having to delete what is already there. |
|
|
|
|
|
|
|
|
|
|
When accepting free-form text input from the users, you often need to perform some data validation before proceeding. For example, if you have a text box for Phone Number, you can use code to check the length of the phone number or even to verify that the users entered any information at all. |
|
|
|
|
|
|
|
|
A new feature in Visual Basic 6 is the Validate event of a text box. This event procedure can contain code that validates the information in the associated control. It is triggered by another control whose CausesValidation property is set to True. For example, the following line of code displays a message box if a text box does not contain numeric data: |
|
|
|
|
|
|
|
|
If Not IsNumeric(Text1) Then MsgBox "please enter a number" |
|
|
|
|
|
|
|
|
To test this event, create a new Standard EXE project. Place a command button and a text box on the form, and enter the preceding line of code in the text box's Validate event. As you use the Tab key to move away from the text box, the message is displayed unless you enter a number. Setting the CausesValidation property of the command button to False turns off the Validate event. |
|
|
|
|
|
|
|
|
Controls for Making Choices |
|
|
|
|
|
|
|
|
Previously, you learned how to acquire input from users through the use of a text box. This approach works well for a number of data-gathering needs. But what if you just want a simple piece of information, such as "Do you own a car?" or "What is your marital status?" In these cases, the choice of answers you want to provide is limited to two or, at most, a few fixed |
|
|
|
|
|